package pers.qianyu.month_202101.date_20210122;

import org.junit.*;

import java.util.*;
import java.util.stream.*;

/**
 * 989. 数组形式的整数加法
 * https://leetcode-cn.com/problems/add-to-array-form-of-integer/
 *
 * @author mizzle rain
 * @date 2021-01-22 20:53
 */
public class AddToArrayForm {
    public List<Integer> addToArrayForm(int[] A, int K) {
        if (K == 0) return Arrays.stream(A).boxed().collect(Collectors.toList());
        LinkedList<Integer> res = new LinkedList<>();
        int take = 0;
        int cur = A.length - 1;
        while (K > 0 || cur >= 0 || take > 0) {
            int a = cur >= 0 ? A[cur] : 0;
            int b = K > 0 ? K % 10 : 0;
            int t = a + b + take;
            if (t >= 10) {
                take = 1;
                t %= 10;
            } else take = 0;
            res.addFirst(t);
            K /= 10;
            cur--;
        }
        return res;
    }

    @Test
    public void test1() {
        List<Integer> result = new AddToArrayForm()
                .addToArrayForm(new int[]{1, 2, 0, 0}, 34);
        System.out.println("result = " + result);
    }

    @Test
    public void test2() {
        int[] arr = new int[]{9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
        int a = 1;
        List<Integer> result = new AddToArrayForm().addToArrayForm(arr, a);
        System.out.println("result = " + result);
    }
}
